﻿using RXY_Mall.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RXY_Mall.Application.Goods.Dto
{
    public static class GoodsDtoExtension
    {
        public static void ToGoodsList(this GoodsList dto, gw_goods entity)
        {
            dto.id = entity.id;
            dto.name = entity.name;
            dto.add_time = entity.add_time;
            if (entity.gw_shop != null)
            {
                dto.shopName = entity.gw_shop.name;
            }
            else
            {
                dto.shopName = "";
            }
            if (null != entity.gw_goods_category)
            {
                dto.categoryName = entity.gw_goods_category.name;
            }
            else
            {
                dto.categoryName = "";
            }
            if (null != entity.gw_goods_property)
            {
                dto.settlement_price = entity.gw_goods_property.settlement_price == null ? 0 : (decimal)entity.gw_goods_property.settlement_price;
                dto.sale_price = entity.gw_goods_property.sale_price;
                //if (entity.type_id == 2|| entity.type_id == 4)
                //{
                //    dto.sale_price = entity.gw_goods_property.settlement_price == null || entity.gw_goods_property.settlement_price == 0 ? entity.gw_goods_property.sale_price : (decimal)entity.gw_goods_property.settlement_price;
                //}
                dto.market_price= entity.gw_goods_property.market_price;
                //dto.service_rate = (decimal)entity.gw_goods_property.service_rate;
                dto.sale_count = entity.gw_goods_property.sale_count;
                dto.comment_count = entity.gw_goods_property.comment_count;
                dto.published = entity.gw_goods_property.published;
                dto.is_sale = entity.gw_goods_property.is_sale;
                dto.is_new = entity.gw_goods_property.is_new;
                dto.is_top = entity.gw_goods_property.is_top;
                dto.sort = entity.gw_goods_property.sort;
            }
            else
            {
                dto.sale_price = 0;
                dto.service_rate = 0;
                dto.settlement_price = 0;
                dto.sale_count = 0;
                dto.comment_count = 0;
                dto.published = 0;
                dto.is_sale = false;
                dto.is_new = false;
                dto.is_top = false;
                dto.sort = 0;
            }
            if (null != entity.gw_goods_picture)
            {
                dto.default_img = entity.gw_goods_picture.Where(i => i.is_default).Select(i => i.url).FirstOrDefault();
            }
            else
            {
                dto.default_img = "";
            }
        }

        public static void ToGoodsDto(this GoodsDto dto, gw_goods entity)
        {
            dto.id = entity.id;
            dto.name = entity.name;
            dto.add_time = entity.add_time;
            //if (entity.gw_shop != null)
            //{
            //    dto.shopName = entity.gw_shop.name;
            //}
            //else
            //{
            //    dto.shopName = "";
            //}
            //if (null != entity.gw_goods_category)
            //{
            //    dto.categoryName = entity.gw_goods_category.name;
            //}
            //else
            //{
            //    dto.categoryName = "";
            //}
            if (null != entity.gw_goods_property)
            {
                dto.settlement_price = entity.gw_goods_property.settlement_price==null?0:(decimal)entity.gw_goods_property.settlement_price;
                dto.sale_price = entity.gw_goods_property.sale_price;
                //dto.service_rate = (decimal)entity.gw_goods_property.service_rate;
                dto.sale_count = entity.gw_goods_property.sale_count;
                dto.comment_count = entity.gw_goods_property.comment_count;
                dto.published = entity.gw_goods_property.published;
                dto.is_sale = entity.gw_goods_property.is_sale;
                dto.is_new = entity.gw_goods_property.is_new;
                dto.is_top = entity.gw_goods_property.is_top;
                dto.sort = entity.gw_goods_property.sort;
            }
            else
            {
                dto.sale_price = 0;
                dto.service_rate = 0;
                dto.sale_count = 0;
                dto.comment_count = 0;
                dto.published = 0;
                dto.is_sale = false;
                dto.is_new = false;
                dto.is_top = false;
                dto.sort = 0;
            }
            if (null!=entity.gw_goods_picture)
            {
                dto.default_img = entity.gw_goods_picture.Where(i=>i.is_default).Select(i=>i.url).FirstOrDefault();
            }
            else
            {
                dto.default_img = "";
            }
            if (null != entity.gw_goods_sku)
            {
                dto.all_stock_count = entity.gw_goods_sku.Where(i =>! i.deleted).Sum(i=>i.all_stock);
                dto.stock_count = (int)entity.gw_goods_sku.Where(i => !i.deleted).Sum(i => i.stock_quantity);
                dto.sku_count = entity.gw_goods_sku.Count(i=>!i.deleted);
            }
            else
            {
                dto.all_stock_count = 0;
                dto.stock_count = 0;
                dto.sku_count = 0;
            }
        }
        public static void ToGoodsInfo(this GoodsInput dto, gw_goods entity)
        {
            dto.id = entity.id;
            dto.name = entity.name;
            dto.short_title = entity.short_title;
            //dto.category_id = (int)entity.category_id;
            dto.type_id = entity.type_id;
            dto.shop_id = entity.shop_id;
            dto.summary = entity.summary;
            dto.description = entity.description;
            dto.limit_quantity = entity.limit_quantity;
            dto.notify_admin_quantity = entity.notify_admin_quantity;
            //dto.category_id1 = (int)entity.category_id1;
            //dto.category_id2 = (int)entity.category_id2;
            //dto.category_id3 = entity.category_id3==null?0: (int)entity.category_id3;
            dto.weight = entity.weight==null?0:(decimal)entity.weight;
            dto.freight_template_id = entity.freight_template_id==null?-1 : (int)entity.freight_template_id;
            if (entity.gw_base_brand!=null)
            {
                dto.brand = entity.gw_base_brand.name;
            }
            else
            {
                dto.brand = "";
            }
            dto.remark = entity.remark;
            if (entity.gw_shop != null)
            {
                dto.shopName = entity.gw_shop.name;
            }
            else
            {
                dto.shopName = "";
            }
            if (entity.type_id==2|| entity.type_id == 4)
            {
                if (entity.gw_goods_rule_free != null)
                {
                    dto.require_num = entity.gw_goods_rule_free.require_num;
                    dto.duration = entity.gw_goods_rule_free.duration;
                    dto.start_time = entity.gw_goods_rule_free.start_time;
                    dto.end_time = entity.gw_goods_rule_free.end_time;
                }
                else
                {
                    dto.require_num = 3;
                    dto.duration = 24;
                    dto.start_time = DateTime.Now;
                    dto.end_time = null;

                }
            }
            else
            {
                if (entity.gw_goods_rule_group != null)
                {
                    //dto.group_price = (decimal)entity.gw_goods_rule_group.group_price;
                    dto.red_packet = (decimal)entity.gw_goods_rule_group.red_packet;
                    dto.require_num = entity.gw_goods_rule_group.require_num;
                    dto.duration = entity.gw_goods_rule_group.duration;
                    dto.start_time = entity.gw_goods_rule_group.start_time;
                    dto.end_time = entity.gw_goods_rule_group.end_time;
                }
                else
                {
                    //dto.group_price = 0;
                    dto.red_packet = 0;
                    dto.require_num = 3;
                    dto.duration = 24;
                    dto.start_time = DateTime.Now;
                    dto.end_time = null;

                }
            }
            
            if (null != entity.gw_goods_property)
            {
                dto.settlement_price = entity.gw_goods_property.settlement_price==null?0:(decimal)entity.gw_goods_property.settlement_price;
                dto.sale_price = entity.gw_goods_property.sale_price;
                //if (dto.type_id==2|| dto.type_id == 4)
                //{
                //    dto.sale_price = entity.gw_goods_property.settlement_price == null || entity.gw_goods_property.settlement_price ==0? entity.gw_goods_property.sale_price : (decimal)entity.gw_goods_property.settlement_price;
                //}
                //dto.service_rate = (decimal)entity.gw_goods_property.service_rate;
                dto.market_price =entity.gw_goods_property.market_price;
                dto.is_new = entity.gw_goods_property.is_new;
                dto.is_top = entity.gw_goods_property.is_top;
                dto.sort = entity.gw_goods_property.sort;
            }
            else
            {
                dto.settlement_price = 0;
                dto.sale_price = 0;
                dto.service_rate = 0;
                dto.market_price = 0;
                dto.is_new = false;
                dto.is_top = false;
                dto.sort = 99;
            }
            if (entity.gw_goods_service.Count>0)
            {
                string gs = "";
                foreach (var item in entity.gw_goods_service)
                {
                    gs+= ","+item.id;
                }
                if (gs.Length>0)
                {
                    dto.goods_service = gs.Substring(1);
                }
            }
        }
    }
}
